package com.platform.modules.mall.goods.utils;


import com.platform.common.utils.Constant;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.activity.entity.bo.ActivityGoshoppingBo;
import com.platform.modules.mall.entity.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 初始化订单工具类
 */
public class CreateOrderUtil {


    /**
     * 直接创建无需购买的卡券订单
     * 创建卡券订单：卡券订单是定时器发送发券
     *                  MallOrderEntity::getPayStatus, Constant.PayStatus.YFK.getValue())   //已付款
     *                 .eq(MallOrderEntity::getOrderStatus, Constant.OrderStatus.YFK.getValue())    //orderstatus 为已付款
     *                 .eq(MallOrderEntity::getOrderType, Constant.CARD_ORDER).list();      //type为卡券类型
     *
     * @param activity
     * @param goods
     * @param bo
     * @return
     */
    public static MallOrderEntity initOrder(MallActivityEntity activity, MallGoodsEntity goods, ActivityGoshoppingBo bo){
        final MallOrderEntity entity =new MallOrderEntity();
        String orderSn = StringUtils.generateOrderNumber();
        entity.setAddTime(new Date());
        entity.setGoodsId(goods.getId());
        entity.setGoodsName(goods.getName());
        entity.setUserId(bo.getUserId());
        entity.setMobile(bo.getPhone());
        entity.setOrderPrice(goods.getRetailPrice());
        entity.setOrderSn(orderSn);
        entity.setActivityId(activity.getId());
        entity.setExpireTime(DateUtils.addDateMinutes(new Date(), 30));
        entity.setOrderType(goodsTypeReplaceOrderType(goods.getGoodsType()));

        /*付费活动*/
        if (Constant.IsNot.YES.getValue().equals(activity.getNeedToPay())) {
            /*放入优惠标识*/
            BigDecimal multiply = goods.getRetailPrice().multiply(new BigDecimal(bo.getNumber()));
            entity.setActualPrice(multiply);
            entity.setActivityType(StringUtils.isNotBlank(goods.getGoodsTag())?goods.getGoodsTag():activity.getGoodsTag());
            entity.setOrderStatus(Constant.OrderStatus.DFK.getValue());
            entity.setPayStatus(Constant.PayStatus.WFK.getValue());
        }else{
            /*免费活动*/
            entity.setActualPrice(new BigDecimal("0"));
            entity.setPayType(Constant.PayType.NO_PAY.getValue());
            paySuccessUpdateOrderStatus(entity);
        }

        return entity;
    }

    /**
     * 支付完成修改订单信息
     * @param order
     */
    public static void paySuccessUpdateOrderStatus(MallOrderEntity order){
        order.setOrderStatus(Constant.OrderStatus.YFK.getValue());
        order.setPayStatus(Constant.PayStatus.YFK.getValue());
    }

    /**
     * 支付完成修改订单信息
     * @param order
     */
    public static void payFailUpdateOrderStatus(MallOrderEntity order){
        order.setOrderStatus(Constant.OrderStatus.YQX.getValue());
        order.setPayStatus(Constant.PayStatus.WFK.getValue());
    }

    /*无效的商品，谢谢惠顾，只用于生成订单*/
    private static final int GOODS_TYPE_WUXIAO=2;
    /*卡卷商品，需要创建卡券订单，需要定时器描述订单发送卡券*/
    private static final int GOODS_TYPE_CARD=1;

    public static int goodsTypeReplaceOrderType(int goodsType){
        if (GOODS_TYPE_WUXIAO==goodsType){
            return OrderType.WU_XIAO_ORDER.getValue();
        }else if (GOODS_TYPE_CARD==goodsType){
            return OrderType.CARD_ORDER.getValue();
        }
        return OrderType.PU_TONG_ORDER.getValue();
    }

    public enum OrderType{

        PU_TONG_ORDER(1),
        ZI_TI_ORDER(2),
        MIAO_SHA_ORDER(3),
        CARD_ORDER(4),
        JI_FEN_ORDER(5),
        WU_XIAO_ORDER(6);

        private int value;
        OrderType(int v){
            this.value=v;
        }
        public int getValue(){
            return value;
        }
    }

    public static void main(String[] args) {
        MallGoodsEntity goods =new MallGoodsEntity();
        goods.setRetailPrice(new BigDecimal(-1));


    }

}
